﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Marbles.Core.Connectors;
using System.Data;
using Marbles.Core.Content.Sites;

namespace Marbles.Core.Content.SiteMap
{
    public class WebSiteMap : ICollection<WebSiteMapNode>
    {

        public int Count
        {
            get { return _items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public WebSiteMapNode this[int index] { get { return _items[index]; } set { _items[index] = value; } }
        public string SiteId { get { return _SiteId; } set { _SiteId = value; } }

        private List<WebSiteMapNode> _items = null;
        private string _SiteId = string.Empty;

        public WebSiteMap()
        {
            _items = new List<WebSiteMapNode>();
            _SiteId = string.Empty;
        }

        public WebSiteMap(string SiteId, bool publishedonly)
            : this()
        {
            _SiteId = SiteId;
            LoadSiteMap(null, SiteId, this, "", publishedonly);
        }

        public WebSiteMap(Pages.PageDefinition Parent, bool publishedonly)
            : this()
        {
            _SiteId = Parent.SiteId;
            LoadSiteMap(null, SiteId, this, Parent.PageName,publishedonly);
        }

        public WebSiteMap(SiteDefinition Web, bool publishedonly)
            : this(Web.SiteId, publishedonly)
        {
        }

        public void Add(WebSiteMapNode item)
        {
            _items.Add(item);
        }

        public void Clear()
        {
            _items.Clear();
        }

        public bool Contains(WebSiteMapNode item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(WebSiteMapNode[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public bool Remove(WebSiteMapNode item)
        {
            return _items.Remove(item);
        }

        public IEnumerator<WebSiteMapNode> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        private void LoadSiteMap(WebSiteMapNode parent, string SiteId, WebSiteMap List, string ParentPageName, bool publishedonly)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT parentpagename, pagename, title, description, publishdate, published FROM pages p WHERE siteid = @siteid AND parentpagename = @parentpagename AND ((@published = 1 AND p.published = 1) OR (@published = 0))");
            cmd.AddParameter("@parentpagename", ParentPageName);
            cmd.AddParameter("@siteid", SiteId);
            cmd.AddParameter("@published", publishedonly);
            DataTable dt = dbc.FetchDataTable(cmd);
            List.Clear();

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["parentpagename"].ToString().ToLower() == ParentPageName.ToLower())
                {
                    WebSiteMapNode node = new WebSiteMapNode(parent, dr);
                    LoadSiteMap(node, SiteId, node.ChildNodes, dr["pagename"].ToString(), publishedonly);
                    List.Add(node);
                }
            }
        }

        public void GetTreeNodes(TreeNodeCollection nodes, bool showstate)
        {
            nodes.Clear();
            foreach (WebSiteMapNode node in _items)
            {
                TreeNode TreeNode = new TreeNode();
                TreeNode.Value = node.PageName.ToString();
                TreeNode.Text = node.Title;
                TreeNode.ToolTip = node.Title + " (" + node.LocalUrl + ")";
                 
                if (node.HasChildren)
                {
                    GetTreeNodes(node, TreeNode);
                }

                if (showstate)
                {
                    if (node.Published)
                    {
                        TreeNode.Text = "<span style=\"color:green;font-weight:bold;\">" + node.Title + "</span>";
                        TreeNode.ToolTip = node.Title + " (" + node.LocalUrl + ") - Published";
                    }
                }

                nodes.Add(TreeNode);
            }
        }

        private void GetTreeNodes(WebSiteMapNode parent, TreeNode root)
        {
            foreach (WebSiteMapNode node in parent.ChildNodes)
            {
                TreeNode TreeNode = new TreeNode();
                TreeNode.Value = node.PageName.ToString();
                TreeNode.Text = node.Title;
                TreeNode.ToolTip = node.Title + " (" + node.LocalUrl + ")";

                if (node.HasChildren)
                {
                    GetTreeNodes(node, TreeNode);
                }

                root.ChildNodes.Add(TreeNode);
            }
        }
    }
}
